package airthmetic.exercise.string;

import java.util.Arrays;
import java.util.HashMap;

public class _1122_数组的相对排序 {

    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        HashMap<Integer, Integer> map = new HashMap();
        for(int i=0; i<arr1.length; i++){
            if(map.containsKey(arr1[i])){
                map.put(arr1[i], map.get(arr1[1]) + 1);
            }else{
                map.put(arr1[i], 1);
            }
        }

        int[] res = new int[arr1.length];
        int[] tail = new int[arr1.length];
        int r = 0;
        int t = 0;
        for(int j=0; j<arr2.length; j++){
            if(map.containsKey(arr2[j])){
                int count = map.get(arr2[j]);
                for(int k=0; k<count; k++){
                    res[r++] = arr2[j];
                }
            }else{
                tail[t++] = arr2[j];
            }
        }

        int resL = res.length;
        int tailL = tail.length;
        System.arraycopy(tail,0, res,resL,tail.length);
        return res;


    }
}
